package com.zzyl.controller;

import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.service.ResourceService;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zzyl.base.ResponseResult.success;

/**
 * @author Jun
 */
@RestController
@RequestMapping("/resource")
@Api(tags = "资源菜单管理")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @PostMapping("/list")
    @ApiOperation("资源列表")
    public ResponseResult<List<ResourceVo>> resourceList(@RequestBody ResourceDto resourceDto) {
        List<ResourceVo> list = resourceService.list(resourceDto);
        return success(list);
    }

    @PostMapping("/tree")
    @ApiOperation("资源树形")
    public ResponseResult<TreeVo> tree(@RequestBody ResourceDto resourceDto){
        TreeVo treeVo = resourceService.tree(resourceDto);
        return success(treeVo);
    }

    @PutMapping
    @ApiOperation("资源添加")
    public ResponseResult add(@RequestBody ResourceDto resourceDto){
        resourceService.add(resourceDto);
        return success();
    }

    @PostMapping("/enable")
    @ApiOperation("启用禁用")
    public ResponseResult enable(@RequestBody ResourceDto resourceDto){
        resourceService.updateState(resourceDto);
        return success();
    }

    @PatchMapping
    @ApiOperation("资源修改")
    public ResponseResult update(@RequestBody ResourceDto resourceDto){
        resourceService.update(resourceDto);
        return success();
    }

    @DeleteMapping("/{resourceNo}")
    @ApiOperation("资源删除")
    public ResponseResult delete(@PathVariable String resourceNo){
        resourceService.delete(resourceNo);
        return success();
    }
}
